package com.fast.develop.api.rest;

import com.fast.develop.management.ManagementConfiguration;
import com.fast.develop.management.ResourceTreeManager;
import com.fast.develop.management.resource.vo.ResourceVo;
import com.fast.develop.management.resource.vo.TreeVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 莫寅
 * @createTime 2022-06-11 03:26
 * @desc：
 */
@RequestMapping("resource")
public class FileRest {

    private ResourceTreeManager resourceTreeManager;

    public FileRest(ManagementConfiguration managementConfiguration) {
        this.resourceTreeManager = managementConfiguration.getResourceTreeManager();
    }


    @GetMapping(value = "tree")
    public ResponseEntity<List<TreeVo<ResourceVo>>> getResourceTree(){
        List<TreeVo<ResourceVo>> tree = resourceTreeManager.getResourceTree();

        return new ResponseEntity(tree, HttpStatus.OK);
    }

    @PostMapping("folder")
    public ResponseEntity createFolder(String path){
        resourceTreeManager.createFolder(path);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PostMapping("file")
    public ResponseEntity createFile(String path){
        resourceTreeManager.createFile(path);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping("rename/{newName}")
    public ResponseEntity<String> rename(@PathVariable(name = "name") String newName, String path){
        return new ResponseEntity(resourceTreeManager.rename(path, newName), HttpStatus.OK);
    }

    @DeleteMapping
    public ResponseEntity remove(String path){
        resourceTreeManager.remove(path);
        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity read(String path){
        return new ResponseEntity(resourceTreeManager.read(path), HttpStatus.OK);
    }
}
